package com.example.lib_base.utils;

import android.content.Context;
import android.os.Build;
import android.util.Log;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;

import androidx.annotation.RequiresApi;

public class DateTools {

    private static final String TAG = "DateTools";

    /**
     * 获取所在整点的开始时间
     *
     * @param time 时间戳
     * @return 整点开始时间戳
     */
    public static long getPerHourStart(long time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis((long) (time) * 1000);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        return calendar.getTimeInMillis() / 1000;
    }

    /**
     * 获取所在半点的开始时间
     *
     * @param time 时间戳
     * @return 整点开始时间戳
     */
    public static long getHalfHourStart(long time) {
        long temp;
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis((long) (time) * 1000);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        // 传进来的时间戳，如果小于半点的时间，则取整的的时间，如果大于半点时间，则取半点的时间戳
        temp = time < (calendar.getTimeInMillis() / 1000 + 1800) ? (calendar.getTimeInMillis() / 1000) : (calendar.getTimeInMillis() / 1000 + 1800);
        return temp;
    }

    /**
     * 根据时间戳，获取这个时间戳所属的本周的每天的时间戳
     *
     * @param time 秒
     * @return
     */
    public static List<Long> getThisWeekTime(long time) {
        List<Long> timeList = new ArrayList<>();
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.setTimeInMillis(time * 1000);
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        timeList.add(getTimesmorning(calendar.getTime().getTime() / 1000));
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
        timeList.add(getTimesmorning(calendar.getTime().getTime() / 1000));
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
        timeList.add(getTimesmorning(calendar.getTime().getTime() / 1000));
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
        timeList.add(getTimesmorning(calendar.getTime().getTime() / 1000));
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
        timeList.add(getTimesmorning(calendar.getTime().getTime() / 1000));
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
        timeList.add(getTimesmorning(calendar.getTime().getTime() / 1000));
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        timeList.add(getTimesmorning(calendar.getTime().getTime() / 1000));
        return timeList;
    }

    /**
     * 获取指定年月的第一天
     *
     * @param year
     * @param month
     * @return
     */
    public static long getFirstDayOfMonth1(int year, int month) {
        Calendar cal = Calendar.getInstance();
        // 设置年份
        cal.set(Calendar.YEAR, year);
        // 设置月份
        cal.set(Calendar.MONTH, month - 1);
        // 获取某月最小天数
        int firstDay = cal.getMinimum(Calendar.DATE);
        // 设置日历中月份的最小天数
        cal.set(Calendar.DAY_OF_MONTH, firstDay);
        // 格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return getTimesmorning(cal.getTime().getTime() / 1000);
    }

    /**
     * 获取指定年月的最后一天
     *
     * @param year
     * @param month
     * @return
     */
    public static long getLastDayOfMonth1(int year, int month) {
        Calendar cal = Calendar.getInstance();
        // 设置年份
        cal.set(Calendar.YEAR, year);
        // 设置月份
        cal.set(Calendar.MONTH, month - 1);
        // 获取某月最大天数
        int lastDay = cal.getActualMaximum(Calendar.DATE);
        // 设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        // 格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return getTimesmorning(cal.getTime().getTime() / 1000);
    }

    /**
     * 获得某天0点时间 (秒级时间戳)
     *
     * @return
     */
    public static long getTimesmorning(long time) {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis((long) time * 1000);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTimeInMillis() / 1000;
    }

    /**
     * 字符串时间转为数据库时间戳
     *
     * @param timeStr
     * @param format
     * @return
     */
    public static long convertStringToTime(String timeStr, String format) {
        SimpleDateFormat mDateFormat = new SimpleDateFormat(format);
        long time = 0;
        try {
            Date date = mDateFormat.parse(timeStr);
            time = date.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return time;
    }

    /**
     * 获取系统当前是24小时制还是12小时制，True24小时制否则则是12小时制
     *
     * @param context
     * @return
     */
    public static boolean get24HourMode(final Context context) {
        return android.text.format.DateFormat.is24HourFormat(context);
    }

    /**
     * 前的6天的时间
     *
     * @return
     */
    public static List todayBefore6DayTime() {
        List<Long> list = new ArrayList<>();
        int temp = -1;
        for (int i = 0; i < 6; i++) {
            Date date = new Date(DateTools.getTimesmorning(System.currentTimeMillis() / 1000) * 1000);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DAY_OF_MONTH, temp);
            date = calendar.getTime();
            long temp_time = date.getTime();
            String time = DateConvertUtils.convertTimeToStringT(temp_time, "yyyy-MM-ddHH:mm:ss");
            list.add(temp_time / 1000);
            temp--;
        }
        return list;
    }


    /**
     * 获取到时分秒的秒级单位
     * @return
     */
    public static int getHourZH(Long timeStamp){
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
        calendar.setTimeInMillis(timeStamp*1000L);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND);
        return hour * 3600 + minute * 60 + second;
    }


    /**
     * 获取到日时分秒的秒级单位
     * @return
     */
    public static int getDayZH(Long timeStamp){
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
        calendar.setTimeInMillis(timeStamp*1000L);
        int day = calendar.get(Calendar.DATE);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND);
        return day*24*3600+hour * 3600 + minute * 60 + second;
    }

    /**
     * 获取指定某一天的开始时间戳
     *
     * @param timeStamp 毫秒级时间戳
     * @param timeZone  如 GMT+8:00
     * @return
     */
    public static Long getDailyStartTime(Long timeStamp, String timeZone) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeZone(TimeZone.getTimeZone(timeZone));
        calendar.setTimeInMillis(timeStamp);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis();
    }

    /**
     * 获取指定某一天的结束时间戳
     *
     * @param timeStamp 毫秒级时间戳
     * @param timeZone  如 GMT+8:00
     * @return
     */
    public static Long getDailyEndTime(Long timeStamp, String timeZone) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeZone(TimeZone.getTimeZone(timeZone));
        calendar.setTimeInMillis(timeStamp);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTimeInMillis();
    }

    /**
     * 获取当月开始时间戳
     *
     * @param timeStamp 毫秒级时间戳
     * @param timeZone  如 GMT+8:00
     * @return
     */
    public static Long getMonthStartTime(Long timeStamp, String timeZone) {
        Calendar calendar = Calendar.getInstance();// 获取当前日期
        calendar.setTimeZone(TimeZone.getTimeZone(timeZone));
        calendar.setTimeInMillis(timeStamp);
        calendar.add(Calendar.YEAR, 0);
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis();
    }

    /**
     * 获取当月的结束时间戳
     *
     * @param timeStamp 毫秒级时间戳
     * @param timeZone  如 GMT+8:00
     * @return
     */
    public static Long getMonthEndTime(Long timeStamp, String timeZone) {
        Calendar calendar = Calendar.getInstance();// 获取当前日期
        calendar.setTimeZone(TimeZone.getTimeZone(timeZone));
        calendar.setTimeInMillis(timeStamp);
        calendar.add(Calendar.YEAR, 0);
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));// 获取当前月最后一天
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTimeInMillis();
    }

    /**
     * 获取当年的开始时间戳
     *
     * @param timeStamp 毫秒级时间戳
     * @param timeZone  如 GMT+8:00
     * @return
     */
    public static Long getYearStartTime(Long timeStamp, String timeZone) {
        Calendar calendar = Calendar.getInstance();// 获取当前日期
        calendar.setTimeZone(TimeZone.getTimeZone(timeZone));
        calendar.setTimeInMillis(timeStamp);
        calendar.add(Calendar.YEAR, 0);
        calendar.add(Calendar.DATE, 0);
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_YEAR, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis();
    }

    /**
     * 获取当年的最后时间戳
     *
     * @param timeStamp 毫秒级时间戳
     * @param timeZone  如 GMT+8:00
     * @return
     */
    public static Long getYearEndTime(Long timeStamp, String timeZone) {
        Calendar calendar = Calendar.getInstance();// 获取当前日期
        calendar.setTimeZone(TimeZone.getTimeZone(timeZone));
        calendar.setTimeInMillis(timeStamp);
        int year = calendar.get(Calendar.YEAR);
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        calendar.roll(Calendar.DAY_OF_YEAR, -1);
        return calendar.getTimeInMillis();
    }

    /**
     * 时间戳转字符串
     *
     * @param timestamp 毫秒级时间戳
     * @param zoneId    如 GMT+8或UTC+08:00
     * @return
     */
    @RequiresApi(api = Build.VERSION_CODES.O)
    public static String timestampToStr(long timestamp, String zoneId) {
        ZoneId timezone = ZoneId.of(zoneId);
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), timezone);
        return localDateTime.toString();
    }



}
